%% cus.sty 
%% Copyright 2023, 2024 Wenjian Chern.
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Wenjian Chern.
%
% This work consists of the files cus.sty, cus.library.bnf.tex, 
% cus.library.box.tex, cus.library.doc.tex, cus.library.ref.tex,
% cus.module.bgfg.tex, cus.module.box.tex, cus.module.buffer.tex,
% cus.module.index.tex, cus.module.layout.tex, cus.module.notes.tex,
% cus.module.pdfmana.tex, cus.module.space.tex, cus.module.struct.tex,
% cus.module.util.tex and cusclass.cls .
\NeedsTeXFormat{LaTeX2e}[2024/11/01]
\def\cus@d@te{2025/03/09}
\def\cus@versi@n{0.0.6}
\ProvidesExplPackage{cus}{\cus@d@te}{\cus@versi@n}{Chinese User Scheme (CUS) basic file}

\def\CusTeX{C\kern-.12em \raise.0466ex\hbox{u}\kern-.1em\lower .4ex\hbox{S}\kern-.15em\TeX}
\def\CusLaTeX{C\kern-.12em \raise.0466ex\hbox{u}\kern-.1em\lower .4ex\hbox{S}\kern-.1em\LaTeX}

%% primitive alternative and latex3 adding
\cs_new_eq:NN \__cus_strcmp:ww \tex_strcmp:D 
\cs_new_eq:NN \__cus_int_let:Nw \tex_mathchardef:D 
\cs_new_eq:NN \__cus_expanded:w \tex_expanded:D
\cs_new_eq:NN \__cus_unexpanded:w \tex_unexpanded:D
\cs_new_eq:NN \__cus_dimexpr:w \tex_dimexpr:D 
\cs_new_eq:NN \__cus_intexpr:w \tex_intexpr:D 

\cs_new:Npn \__cus_e:w { \__cus_expanded:w }
\cs_new:Npn \__cus_o:w { \exp:w \exp_after:wN \exp_end: }
\cs_new:Npn \__cus_d:w { \exp:w \exp_after:wN \exp_after:wN \exp_after:wN \exp_end: }
\cs_new:Npn \__cus_f:w { \exp:w \exp_end_continue_f:w }
\cs_new_protected:Npn \__cus_int_let:cw { \exp_args:Nc \__cus_int_let:Nw }

\int_const:Nn \c__cus_mone_int { -1 }
\int_const:Nn \c__cus_zero_int { 0 }
\int_const:Nn \c__cus_one_int { 1 }
\int_const:Nn \c__cus_two_int { 2 }
\int_const:Nn \c__cus_three_int { 3 }
\int_const:Nn \c__cus_four_int { 4 }
\int_const:Nn \c__cus_five_int { 5 }
\int_const:Nn \c__cus_six_int { 6 }
\int_const:Nn \c__cus_seven_int { 7 }
\int_const:Nn \c__cus_eight_int { 8 }
\int_const:Nn \c__cus_nine_int { 9 }
\int_const:Nn \c__cus_ten_int { 10 }
\int_const:Nn \c__cus_eleven_int { 11 }
\int_const:Nn \c__cus_twelve_int { 12 }
\int_const:Nn \c__cus_thirteen_int { 13 }
\int_const:Nn \c__cus_fourteen_int { 14 }
\int_const:Nn \c__cus_fifteen_int { 15 }
\int_const:Nn \c__cus_sixteen_int { 16 }

\tl_const:Nx \c__cus_other_space_tl { \char_generate:nn { 32 } { 12 } }

\cs_if_free:NT \token_if_cs_word:NTF 
  {
    \prg_new_conditional:Npnn \token_if_cs_word:N #1 { p, T, F, TF }
      {
        \if_catcode:w \exp_not:N #1 \scan_stop:
          \if:w 0 \__cus_strcmp:ww { \exp_not:N #1 } { \token_to_str:N #1 }
            \prg_return_false: \else: \prg_return_true: \fi:
        \else: 
          \prg_return_false:
        \fi:
      }
  }
\cs_if_free:NT \token_if_control_word:NTF 
  {
    \prg_new_conditional:Npnn \token_if_control_word:N #1 { p , T , F , TF }
      {
        \if:w 0 \__cus_strcmp:ww { \exp_not:N #1 } { \token_to_str:N #1 }
          \__token_if_control_word_aux:w 
        \else:
          \if:w \exp_not:N #1 \token_to_str:N #1
            \prg_return_false: \else: \prg_return_true: \fi:
        \fi:
      }
    \cs_new:Npn \__token_if_control_word_aux:w #1 \fi: { \prg_return_false: }
  }
\cs_if_free:NT \int_div_floor:nn
  {
    \cs_new:Npn \__int_div_floor:NwNw #1#2; #3#4;
      {
        \if_meaning:w 0 #1
          0
        \else:
          ( #1#2 - ( \if_meaning:w - #3 - \fi: #3#4 - 1 ) / 2 )
        \fi:
        / #3#4
      }
    \cs_new:Npn \int_div_floor:nn #1#2
      {
        \int_value:w \__int_eval:w
          \exp_after:wN \__int_div_floor:NwNw
          \int_value:w \__int_eval:w #1 \exp_after:wN ;
          \int_value:w \__int_eval:w #2 ;
        \__int_eval_end:
      }
  }
\cs_if_free:NT \int_div_ceil:nn
  {
    \cs_new:Npn \__int_div_ceil:NwNw #1#2; #3#4;
      {
        \if_meaning:w 0 #1
          0
        \else:
          ( #1#2 + ( \if_meaning:w - #3 - \fi: #3#4 - 1 ) / 2 )
        \fi:
        / #3#4
      }
    \cs_new:Npn \int_div_ceil:nn #1#2
      {
        \int_value:w \__int_eval:w
          \exp_after:wN \__int_div_ceil:NwNw
          \int_value:w \__int_eval:w #1 \exp_after:wN ;
          \int_value:w \__int_eval:w #2 ;
        \__int_eval_end:
      }
  }
\cs_if_free:NT \int_mod_abs:nn
  {
    \cs_new:Npn \int_mod_abs:nn #1#2
      {
        \int_value:w \__int_eval:w \exp_after:wN \__int_mod_abs:ww
          \int_value:w \__int_eval:w #1 \exp_after:wN ;
          \int_value:w \__int_eval:w #2 ;
        \__int_eval_end:
      }
    \cs_new:Npn \__int_mod_abs:ww #1#2; #3;
      { 
        \if_meaning:w - #1
          - #1#2 +
        \else:
          #1#2 -
        \fi:
        ( \__int_div_truncate:NwNw #1#2 ; #3 ; ) * #3
      }
  }
\cs_if_free:NT \text_mdfive_hash:n 
  {
    \cs_new:Npn \text_mdfive_hash:n #1 
      { \str_mdfive_hash:e { \text_expand:n {#1} } }
  }
%%

\RequirePackage{ctexhook,ctexpatch}
\RequirePackage{etl}
\RequirePackage{lt3ekeysext}[2024/04/25]
\RequirePackage{updatemarks}

\sys_if_engine_xetex:TF 
  { \tex_chardef:D \cus@TATE = \c_zero_int }
  {
    \cs_if_exist:NF \cus@TATE 
      {
        \cs_if_exist:NT \platex_if_direction_tate:TF
          {
            \platex_if_direction_tate:TF 
              { \tex_chardef:D \cus@TATE = \c_one_int  }
              { \tex_chardef:D \cus@TATE = \c_zero_int }
          }
      }
  }

\scan_new:N \s__cus_mark
\scan_new:N \s__cus_stop
\scan_new:N \s__cus_nil
\quark_new:N \q__cus_mark
\elkernel_quark_new_conditional:Nn \__cus_quark_if_mark:N { TF }
\elkernel_quark_new_conditional:Nn \__cus_quark_if_mark:n { TF }
\cs_new:Npn \__cus_use_none_delimit_by_q_mark:w #1 \q__cus_mark { }
\quark_new:N \q__cus_stop
\elkernel_quark_new_conditional:Nn \__cus_quark_if_stop:N { TF }
\elkernel_quark_new_conditional:Nn \__cus_quark_if_stop:n { TF }
\cs_new:Npn \__cus_use_none_delimit_by_q_stop:w #1 \q__cus_stop { }
\quark_new:N \q__cus_nil
\cs_new:Npn \__cus_use_none_delimit_by_q_nil:w #1 \q__cus_nil { }
\int_new:N \l__cus_tmpa_int
\int_new:N \l__cus_tmpb_int
\int_new:N \l__cus_tmpc_int
\dim_new:N \l__cus_tmpa_dim
\dim_new:N \l__cus_tmpb_dim
\dim_new:N \l__cus_tmpc_dim
\clist_new:N \l__cus_tmpa_clist
\clist_new:N \l__cus_tmpb_clist
\clist_new:N \l__cus_tmpc_clist
\box_new:N \l__cus_tmpa_box
\box_new:N \l__cus_tmpb_box
\box_new:N \l__cus_tmpc_box
\cs_set_eq:NN \cus_toks_new:N \newtoks
\cus_toks_new:N \c_cus_empty_toks
\cus_toks_new:N \l__cus_tmpa_toks
\cus_toks_new:N \l__cus_tmpb_toks
\cus_toks_new:N \l__cus_tmpc_toks
\tl_new:N \l__cus_tmpa_tl
\tl_new:N \l__cus_tmpb_tl
\tl_new:N \l__cus_tmpc_tl
\prop_new:N \l__cus_tmpa_prop
\prop_new:N \l__cus_tmpb_prop
\prop_new:N \l__cus_tmpc_prop
\bool_new:N \l__cus_tmpa_bool
\bool_new:N \l__cus_tmpb_bool
\bool_new:N \l__cus_tmpc_bool
\ior_new:N \g__cus_tmp_ior
\iow_new:N \g__cus_tmp_iow

\int_new:N \l__cus_nest_int 
\tl_new:N \g__cus_pdfstring_disable_tl 
\hook_gput_code:nnn { begindocument } { cus/disable }
  { 
    \cs_if_exist:NT \pdfstringdefDisableCommands
      { \pdfstringdefDisableCommands { \g__cus_pdfstring_disable_tl } }
  }
\cs_new_protected:Npn \cus_add_pdf_disable:n 
  { \tl_gput_right:Nn \g__cus_pdfstring_disable_tl }

\cs_new:Npn \cus@filename { \elkernel_file_name_sanitize:n }
\cs_new_eq:NN \CUSInputFile \cus_file_input:n 

\msg_new:nnn { cus } { unknown-choice-key } 
  { The~choice~`#3'~of~key~`#2'~in~module~`#1'~is~unknown. }
\msg_new:nnn { cus } { unknown-key }
  { The~keys~`#2'~in~module~`#1'~is~unknown. }
\msg_new:nnn { cus } { key-missing-value }
  { The~key~`#1'~is~value~required. }
\msg_new:nnn { cus } { counter-unknown }
  { The~counter~`#1'~is~undefined. }
\msg_new:nnn { cus } { bad-variable }
  { Erroneous~variable~#1 used! }

\NewDocumentCommand \cussetup { >{\TrimSpaces} O{} }
  { \tl_if_empty:nTF {#1} { \keys_set:nn { cus } } { \keys_set:nn { cus/#1 } } }
\NewDocumentCommand \cussetstyle { s >{\TrimSpaces} O{} m +m }
  { 
    \bool_if:nTF {#1}
      {
        \tl_if_empty:nTF {#2} 
          { \keys_define:nn { cus } } { \keys_define:nn { cus/#2 } } 
            { #3 .code:n = {#4} }
      }
      {
        \tl_if_empty:nTF {#2} 
          { \keys_define:nn { cus } } { \keys_define:nn { cus/#2 } } 
            { #3 .meta:n = {#4} }
      }
  }

\msg_new:nnnn { cus } { module-out-of-date } 
  { The~module~`#1'~you~try~to~load~is~out~of~date,~please~update~this~package. }
  { The~current~version~of~module~`#1'~is~`#3',~while~you~try~to~load~version~`#2'. }
\msg_new:nnn { cus } { module-not-exist }
  { The~module~`#1'~you~try~to~load~does~not~exist. }
\cs_new_protected_nopar:Npn \CUSProvideModule #1#2#3#4
  {
    \str_if_exist:cT { cus/module~#1 } { \file_input_stop: }
    \ProvidesFile {#1} [#2~#3~#4]
    \str_gset:cx { cus/module~#1 } {#2}
  }
\cs_new_protected_nopar:Npn \CUSProvideExplModule #1#2#3#4
  {
    \str_if_exist:cT { cus/module~#1 } { \file_input_stop: }
    \ProvidesExplFile {#1} {#2} {#3} {#4}
    \str_gset:cx { cus/module~#1 } {#2}
  }
\cs_new:Npn \cus@ifmoduleloaded #1 { \cs_if_exist:cTF { cus/module~#1 } }
\NewDocumentCommand \CUSLoadModule { +O{} m O{0000/00/00} }
  {
    \cus@ifmoduleloaded {#2} { }
      { 
        \file_if_exist:nTF { cus.module.#2.tex }
          { \elkernel_file_with_options:nnn { cus.module.#2.tex } {#3} {#1} }
          { \msg_error:nnn { cus } { module-not-exist } {#2} }
      }
  }
\msg_new:nnnn { cus } { library-out-of-date } 
  { The~library~`#1'~you~try~to~load~is~out~of~date,~please~update~this~package. }
  { The~current~version~of~library~`#1'~is~`#3',~while~you~try~to~load~version~`#2'. }
\msg_new:nnn { cus } { library-not-exist }
  { The~library~`#1'~you~try~to~load~does~not~exist. }
\cs_new_protected_nopar:Npn \CUSProvideLibrary #1#2#3#4
  {
    \str_if_exist:cT { cus/library~#1 } { \file_input_stop: }
    \ProvidesFile {#1} [#2~#3~#4]
    \str_gset:cx { cus/library~#1 } {#2}
  }
\cs_new_protected_nopar:Npn \CUSProvideExplLibrary #1#2#3#4
  {
    \str_if_exist:cT { cus/library~#1 } { \file_input_stop: }
    \ProvidesExplFile {#1} {#2} {#3} {#4}
    \str_gset:cx { cus/library~#1 } {#2}
  }
\cs_new:Npn \cus@iflibraryloaded #1 { \cs_if_exist:cTF { cus/library~#1 } }
\prop_new:N \g__cus_library_delayed_prop
\prop_new_linked:N \g__cus_library_dependency_prop
\cs_new_protected:Npn \__cus_library_add_dependency:n #1
  {
    \tl_set:Nx \l__cus_tmpc_tl 
      { \prop_item:No \g__cus_library_dependency_prop \CurrentFile }
    \keyval_parse:NNn 
      \__cus_library_add_dependency_aux:n
      \__cus_library_add_dependency_aux:nn
      {#1}
    \clist_remove_duplicates:N \l__cus_tmpc_tl
    \prop_gput:Noo \g__cus_library_dependency_prop \CurrentFile { \l__cus_tmpc_tl }
  }
\cs_new:Npn \__cus_library_add_dependency_aux:n #1
  { \clist_put_right:Nn \l__cus_tmpc_tl {#1} }
\cs_new:Npn \__cus_library_add_dependency_aux:nn #1#2
  { \clist_put_right:Nn \l__cus_tmpc_tl {#1} }
\ekeysdeclarecmd \CUSLibraryDelayedUntil { s m }
  {
    \bool_if:NT \l__cus_load_library_dependency_bool { \RequirePackage {#2} }
    \tl_set:Nx \l__cus_tmp_tl { \tl_range:Nnn \CurrentFile { 13 } { -5 } }
    \bool_if:nT {#1} 
      { 
        \cus_if_class_loaded:F 
          {
            \__cus_load_library_hook:nn \cus_hook_gput_dc:n { \l__cus_tmp_tl }
            \use_i:nnnn \file_input_stop:
          }
      }
    \tl_if_blank:nF {#2}
      {
        \prop_if_in:NoTF \g__cus_library_delayed_prop \l__cus_tmp_tl
          { 
            \@ifpackageloaded {#2} { } 
              { \__cus_load_library_hook:nn { \use_none:n } \l__cus_tmp_tl \file_input_stop: } 
          }
          {
            \prop_gput:Nxx \g__cus_library_delayed_prop \l__cus_tmp_tl {#2}
            \@ifpackageloaded {#2} 
              { }
              { 
                \__cus_load_library_hook:nn 
                  { \hook_gput_code:nnn { package/#2/after } {cus/load} } \l__cus_tmp_tl
                \file_input_stop: 
              }
          }
      }
  }
\cs_new_protected:Npn \__cus_load_library_hook:nn #1#2
  {
    \cs_undefine:c { cus/library~#2 }
    \cs_undefine:c { @pkg-disable@cus.library.#2.tex }
    \cs_undefine:c { ver@cus.library.#2.tex }
    \exp_args:Nne \use:n {#1}
      {
        \__cus_load_library_delayed:nnn { #2 }
          { \exp_not:v { cus/library-options~#2 } }
          { \exp_not:v { cus/library-version~#2 } }
      }
  }
\cs_new_protected:Npn \__cus_load_library_check:nT #1#2
  { 
    \exp_args:Nne \__cus_load_library_check:nnTF {#1}
      { \prop_item:Nn \g__cus_library_dependency_prop { cus.library.#1.tex } }
      {#2} { }
  }
\cs_new_protected:Npn \__cus_load_library_check:nnTF #1#2
  {
    \bool_set_true:N \l__cus_tmpc_bool
    \clist_map_inline:nn {#2}
      {
        \bool_if:NT \l__cus_load_library_dependency_bool
          { \__cus_load_library_do:nnn {##1} { } { } }
        \cus@iflibraryloaded {##1} { }
          {
            \bool_set_false:N \l__cus_tmpc_bool
            \__cus_load_library_hook:nn 
              { \hook_gput_code:nnn { file/cus.library.##1.tex/after } { cus/load } }
              {#1}
          }
      }
    \bool_if:NTF \l__cus_tmpc_bool
  }
\cs_new_protected:Npn \__cus_load_library_delayed:nnn #1#2#3
  {
    \__cus_load_library_check:nT {#1}
      { \__cus_load_library_do:nnn {#1} {#2} {#3} }
  }
\cs_new_protected:Npn \__cus_load_library_do:nnn #1#2#3
  {
    \use:e
      {
        \elkernel_file_with_options:nnn { cus.library.#1.tex }
          {
            \cs_if_exist:cTF { cus/library-version~#1 }
              { \exp_not:v { cus/library-version~#1 } } {#3}
          }
          {
            \cs_if_exist:cT { cus/library-options~#1 }
              { \exp_not:v { cus/library-options~#1 } , } \exp_not:n {#2}
          }
      }
  }
\cs_new_protected:Npn \CUSPassOptionsToLibrary #1#2
  {
    \clist_if_exist:cF { cus/library-options-passed~#2 }
      { \clist_new:c { cus/library-options-passed~#2 } }
    \clist_put_right:cn { cus/library-options-passed~#2 } {#1}
  }
\NewDocumentCommand \CUSLoadLibrary { +O{} m O{0000/00/00} }
  {
    \tl_if_exist:cF { cus/library-options~#2 }
      {
        \tl_gset:cn { cus/library-options~#2 } {#1}
        \tl_gset:cn { cus/library-version~#2 } {#3}
      }
    \__cus_load_library_delayed:nnn {#2} {#1} {#3}
  }
\bool_new:N \l__cus_load_library_dependency_bool
%% fmt (fmt version), engine, package, library of package, class, module, library
\ekeysdeclarecmd \CUSDependency { s }
  {
    \bool_set:Nn \l__cus_load_library_dependency_bool 
      { \bool_lazy_or_p:nn {#1} { \tl_if_empty_p:N \CurrentFile } }
    \keys_set:nn { cus/dependency } 
  }
%%TODO:
\keys_define:nn { cus/dependency }
  {
    package .code:n = \__cus_load_packages:n {#1} ,
    module .code:n  = \__cus_load_module:n {#1} ,
    library .code:n = \__cus_load_library:n {#1} ,
    disable .code:n = \__cus_disable_packages:n {#1} ,
  }
\cs_new_nopar:Npn \__cus_load_packages:n #1
  {
    \clist_map_inline:nn {#1}
      {
        \tl_if_in:nnTF {##1} { | }
          {
            \seq_set_split:Nnn \l__cus_tmp_seq { | } {##1} 
            \tl_set:Nx \l__cus_tmp_tl { \seq_item:Nn \l__cus_tmp_seq { 1 } }
          }
          { \tl_set:Nn \l__cus_tmp_tl {##1} }
        \exp_args:No \tl_if_head_eq_charcode:nNTF { \l__cus_tmp_tl } ( % )
          {
            \cs_set:Npn \__cus_tmp:w (####1) ####2 \s_stop 
              {
                \tl_set:Nn \l__cus_tmp_tl {####2}
                \tl_set:Nn \l__cus_tmpb_tl {####1}
              }
            \exp_after:wN \__cus_tmp:w \l__cus_tmp_tl \s_stop
            \cs_set:Npn \__cus_tmp:w { \exp_args:No \CUSCaseLoadPackage \l__cus_tmpb_tl }
          }
          { \cs_set:Npn \__cus_tmp:w { \RequirePackage } }
        \tl_set:Nx \l__cus_tmp_tl { \exp_args:No \cus_split_bracket_head:n \l__cus_tmp_tl }
        \@ifpackageloaded { \cus_remove_bracket_head:N \l__cus_tmp_tl } { }
          { \exp_after:wN \__cus_tmp:w \l__cus_tmp_tl }
      }
  }
\cs_new_nopar:Npn \__cus_load_module:n #1 { }
\cs_new_nopar:Npn \__cus_load_library:n #1
  { 
    \__cus_library_add_dependency:n {#1}
    \keyval_parse:NNn \__cus_load_library_aux:n \__cus_load_library_aux:nn {#1}
    \exp_args:No \str_if_in:nnT
      { \exp_after:wN \s__cus_mark \CurrentFile } { \s__cus_mark cus.library. }
      {
        \exp_args:Ne \__cus_load_library_check:nnTF 
          { \tl_range:Nnn \CurrentFile { 13 } { -5 } } {#1} 
          { } 
          { 
            \__cus_load_library_hook:nn { \use_none:n } 
              { \tl_range:Nnn \CurrentFile { 13 } { -5 } }
            \file_input_stop: 
          } 
      }
  }
\cs_new:Npn \__cus_load_library_aux:n #1 { \__cus_load_library_aux:nn {#1} { } }
\cs_new:Npn \__cus_load_library_aux:nn #1
  { 
    \exp_last_unbraced:Ne \__cus_load_library_aux:nwn 
      { \cus_split_bracket_tail_default:nn {#1} { 0000/00/00 } }
  }
\cs_new:Npn \__cus_load_library_aux:nwn #1[#2]#3 { \CUSLoadLibrary [{#3}] {#1} [{#2}] }
\msg_new:nnn { cus } { package-disable } 
  { Compiling~aborted,~you~should~not~use~package~`#1'. }
\cs_new_nopar:Npn \__cus_disable_packages:n #1
  {
    \clist_map_inline:nn {#1}
      { 
        \@ifpackageloaded {##1} { \msg_fatal:nn { cus } { package-disable } }
          { \disable@package@load {##1} { } }
      }
  }
\NewDocumentCommand \CUSCaseLoadPackage { m o m o }
  {
    \cs_if_exist:cT {#1}
      {
        \int_if_odd:nT { \use:c {#1} }
          {
            \tl_if_novalue:nTF {#2}
              {
                \tl_if_novalue:nTF {#4}
                  { \RequirePackage {#3} }
                  { \RequirePackage {#3} [#4] }
              }
              {
                \tl_if_novalue:nTF {#4}
                  { \RequirePackage [{#2}] {#3} }
                  { \RequirePackage [{#2}] {#3} [#4] }
              }
          }
      }
    \scan_stop:
  }


\cs_new_protected:Npn \cus_pkg_code:nnn #1#2#3 % add #3 after pkg #1, default #2
  {
    #2
    \hook_gput_code:nnn { package/#1/after } { cus/pkg/after } {#3}
  }
\cs_new_protected:Npn \cus_pkg_code:nn #1#2
  { \cus_pkg_code:nnn {#1} {#2} {#2} }


\CUSLoadModule { ltx }
\CUSLoadModule { util }
\CUSLoadModule { algo }

\group_begin:
\cs_set:Npn \__cus_latex_hook_new:nnNn #1#2#3#4
  {
    \cus_toks_new:N #3
    \hook_gput_code:nnn {#1} {#2} { \cus_toks_use_gclear:N #3 }
    \cs_new_protected:cpn { cus_hook_gput_ #4 :n } { \cus_toks_gput_right:Nn #3 }
    \cs_new_protected:cpn { cus_hook_gput_ #4 :w } ##1 \cus_hook_end:
      { \cus_toks_gput_right:Nn #3 {##1} }
  }
\clist_set:Nn \l__cus_hook_tl
  {
    {begindocument/before}  {cus/document}\g__cus_preamble_end_toks     {pe} ,
    {begindocument}         {cus/document}\g__cus_aux_end_toks          {ae} ,
    {begindocument/end}     {cus/document}\g__cus_document_starting_toks{ds} ,
    {enddocument}           {cus/document}\g__cus_document_ending_toks  {de} ,
    {enddocument/afterlastpage}{cus/document}\g__cus_after_lastpage_toks{lp} ,
    {enddocument/afteraux}  {cus/document}\g__cus_after_aux_toks        {aa} ,
    {shipout/after}         {cus/reset}   \g__cus_page_set_toks         {pg} ,
    {class/after}           {cus/initial} \g__cus_class_hook_toks       {dc} ,
  }
\clist_map_inline:Nn \l__cus_hook_tl { \__cus_latex_hook_new:nnNn #1 }
\group_end:

\cs_new_eq:NN \cus_if_preamble:TF \use_i:nn
\cs_new_eq:NN \cus_if_document:TF \use_ii:nn
\cs_new:Npn \cus_if_preamble:T #1 { \cus_if_preamble:TF {#1} { } }
\cs_new:Npn \cus_if_preamble:F { \cus_if_preamble:TF { } }
\cs_new:Npn \cus_if_document:T #1 { \cus_if_document:TF {#1} { } }
\cs_new:Npn \cus_if_document:F { \cus_if_document:TF { } }
\prg_new_conditional:Npnn \cus_if_class_loaded: { T, F, TF }
  {
    \tl_if_head_eq_meaning:VNTF \normalsize { \@latex@error }
      { \prg_return_false: } { \prg_return_true: }
  }
\cs_new_protected:Npn \cus_after_class:n
  { \cus_if_class_loaded:TF { \use:n } { \cus_hook_gput_dc:n } }

\cus_hook_gput_ae:n 
  {
    \cs_gset_eq:NN \cus_if_preamble:TF \use_ii:nn
    \cs_gset_eq:NN \cus_if_document:TF \use_i:nn
    \hook_gput_code:nnn { shipout/before } { cus/layout/marking } { \__cus_layout_marking: }
  }


\cs_new_protected:Npn \cus_patch_failure:N #1
  { \msg_error:nnx { cus } { patch-failure } { \token_to_str:N } }
\msg_new:nnn { cus } { patch-failure } { Patch~command~`#1'~failure. }

\CUSLoadModule { counter }
\CUSLoadModule { space }
\CUSLoadModule { buffer }
\CUSLoadModule { box }
\CUSLoadModule { pdfmana }
\cus_after_class:n 
  {
    \CUSLoadModule { package }
    \CUSLoadModule { bgfg }
    \CUSLoadModule { layout }
    % \CUSLoadModule { notes }
    \CUSLoadModule { struct }
  }


\cus_pkg_code:nnn { array } { }
  {
    \newcolumntype{v}[1]{}
    \def\NC@rewrite@v#1#{\cus@NC@rewrite@v{#1}}
    \def\cus@NC@rewrite@v #1#2
      { \NC@find >{ \varwidth #1 {#2} \arraybackslash } c <{ \endvarwidth } }
  }


\keys_define:nn { cus/option }
  {
    library .code:n = \__cus_load_library:n {#1} ,
    draft .bool_set:N = \l_cus_draft_bool ,
  }


\cus_pkg_code:nnn { hologo } { }
  {
    \def\CusTeX{\hologo{CusTeX}}
    \def\HoLogo@CusTeX#1{C\kern-.12em \raise.0466ex\hbox{u}\kern-.1em\lower .4ex\hbox{S}\kern-.15em\hologo{TeX}}
    \def\HoLogoBkm@CusTeX#1{Cus\hologo{TeX}}
    \def\CusLaTeX{\hologo{CusLaTeX}}
    \def\HoLogo@CusLaTeX#1{C\kern-.12em \raise.0466ex\hbox{u}\kern-.1em\lower .4ex\hbox{S}\kern-.1em\LaTeX}
    \def\HoLogoBkm@CusLaTeX#1{Cus\hologo{LaTeX}}
    \cus_pkg_code:nnn { unicode-math } { }
      {
        \ctex_patch_cmd:Nnn \HoLogo@LaTeXe
          { \hbox { \HOLOGO@MathSetup 2 $ _{ \textstyle \varepsilon } $ } }
          {
            \hbox
              {
                \mathsurround 0pt \relax
                2
                \if b \expandafter \@car \f@series \@nil
                  $ _{ \textstyle \symbf { \varepsilon } } $
                \else
                  $ _{ \textstyle \varepsilon } $
                \fi
              }
          }
        \cs_set_eq:cN { HoLogo@LaTeX2e } \HoLogo@LaTeXe
        \cs_set_nopar:Npn \HoLogo@eTeX #1 
          {
            \ltx@mbox 
              {
                \mathsurround 0pt \relax
                $ \symbf { \varepsilon } $
                -
                \HOLOGO@NegativeKerning{-T,T-,To}
                \hologo{TeX}
              }
          }
      }
  }



\ProcessKeyOptions [ cus/option ]